package com.hnl.action.talent;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.core.aware.HNLTalentAware;
import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.TalentDAO;

@SuppressWarnings("serial")
public class DeleteTalent extends HttpServlet implements HNLAuthAware,
		HNLTalentAware, MiscTypeAware, HNLPageDispatch {
	public static final Logger LOG = LoggerFactory
			.getLogger(DeleteTalent.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String[] selectedPersonalCheckboxes = req
					.getParameterValues("personalTableCheckbox");
			String[] selectedAllCheckboxes = req
					.getParameterValues("allTableCheckbox");

			TalentDAO talentDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getTalentDAO();

			// this will delete any checkbox checked regardless of table
			if (selectedPersonalCheckboxes != null) {
				for (String talentID : selectedPersonalCheckboxes) {
					talentDAO.deleteTalent(Integer.valueOf(talentID));
				}
			}

			if (selectedAllCheckboxes != null) {
				for (String talentID : selectedAllCheckboxes) {
					talentDAO.deleteTalent(Integer.valueOf(talentID));
				}
			}

			RequestDispatcher dispatcher = req
					.getRequestDispatcher(DASHBOARD_PAGE);
			dispatcher.forward(req, resp);
		} catch (DAOException e) {
			LOG.error("Error to delete Talent", e);
		}
	}

}
